import mediaquery from '@ohos.mediaquery'
export default class MediaQueryUtil{
  private static  smListener:mediaquery.MediaQueryListener=mediaquery.matchMediaSync('(320vp<=width<600vp)')
  private static mdListener:mediaquery.MediaQueryListener=mediaquery.matchMediaSync('(600vp<=width<840vp)')
  private static lgListener:mediaquery.MediaQueryListener=mediaquery.matchMediaSync('(840vp<=width)')

  static smListenerCallback(result:mediaquery.MediaQueryResult){
      if(result.matches){
        this.updateCurrentScreen('sm')
      }
  }
  static mdListenerCallback(result:mediaquery.MediaQueryResult){
    if(result.matches){
      this.updateCurrentScreen('md')
    }
  }
  static lgListenerCallback(result:mediaquery.MediaQueryResult){
    if(result.matches){
      this.updateCurrentScreen('lg')
    }
  }
  static updateCurrentScreen(screenSize:string){
    AppStorage.SetOrCreate('screenSize',screenSize)
  }
  static register(){
    this.smListener.on('change',this.smListenerCallback.bind(this))
    this.mdListener.on('change',this.mdListenerCallback.bind(this))
    this.lgListener.on('change',this.lgListenerCallback.bind(this))
  }
  static unRegister(){
    this.smListener.off('change',this.smListenerCallback.bind(this))
    this.mdListener.off('change',this.mdListenerCallback.bind(this))
    this.lgListener.off('change',this.lgListenerCallback.bind(this))
  }
}